package com.yalemang.photovalut.base.sql;

import android.content.Context;

import com.ellen.dhcsqlitelibrary.table.impl.ZxyTable;
import com.ellen.dhcsqlitelibrary.table.operate.TotalListener;
import com.ellen.sqlitecreate.createsql.delete.DeleteTableColumn;
import com.ellen.sqlitecreate.createsql.delete.DeleteTableDataRow;
import com.ellen.sqlitecreate.createsql.helper.WhereSymbolEnum;
import com.ellen.sqlitecreate.createsql.where.Where;
import com.yalemang.framework.log.LogX;
import com.yalemang.photovalut.base.sql.entity.Folder;
import com.yalemang.photovalut.base.sql.entity.ResFile;

import java.util.List;

public class SQLManager {

    private AppLibrary appLibrary;
    private volatile static SQLManager instance;
    private final static String SQL_LIBRARY_NAME = "photo_valut";
    private final static String PHOTO_FOLDER_MANAGER_TABLE_NAME = "photo_folder_manager";
    private final static String VIDEO_FOLDER_MANAGER_TABLE_NAME = "video_folder_manager";
    private final static int SQL_LIBRARY_VERSION = 1;
    private final static String PHOTO_FOLDER_TABLE_NAME_SUFFIX = "photo_";
    private final static String VIDEO_FOLDER_TABLE_NAME_SUFFIX = "video_";

    private final static String PHOTO_FOLDER_FATHER_PATH = "photo";
    private final static String VIDEO_FOLDER_FATHER_PATH = "video";

    private FolderManagerTable photoFolderManagerTable;
    private FolderManagerTable videoFolderManagerTable;

    public FolderManagerTable getPhotoFolderManagerTable() {
        return photoFolderManagerTable;
    }

    public FolderManagerTable getVideoFolderManagerTable() {
        return videoFolderManagerTable;
    }

    private SQLManager(){

    }

    public static SQLManager getInstance(){
        if (instance == null){
            synchronized (SQLManager.class){
                if (instance == null){
                    instance = new SQLManager();
                }
            }
        }
        return instance;
    }

    public void init(Context context){
        ZxyTable.setTotalListener(new TotalListener() {
            @Override
            public void exeSql(String tableName, String sql) {
                LogX.INSTANCE.d("SQLLog" + tableName + " : " + sql , true);
            }
        });

        appLibrary = new AppLibrary(context, SQL_LIBRARY_NAME, SQL_LIBRARY_VERSION);

        photoFolderManagerTable = new FolderManagerTable(PHOTO_FOLDER_MANAGER_TABLE_NAME);
        videoFolderManagerTable = new FolderManagerTable(VIDEO_FOLDER_MANAGER_TABLE_NAME);
        photoFolderManagerTable.onCreateTableIfNotExits();
        videoFolderManagerTable.onCreateTableIfNotExits();
    }

    public AppLibrary getAppLibrary() {
        return appLibrary;
    }

    public boolean queryPhotoFolder(String folderName){
        return photoFolderManagerTable.searchByMajorKey(folderName) != null;
    }

    public boolean createNewPhotoFolder(String folderName){
        if (queryPhotoFolder(folderName)){
            return false;
        }

        Folder folder = new Folder();
        folder.setFolderName(folderName);
        folder.setFolderFatherPath(PHOTO_FOLDER_FATHER_PATH);
        folder.setSize(0);
        folder.setLength(0);
        folder.setLock(false);
        photoFolderManagerTable.saveData(folder);
        return true;
    }

    public void removePhotoFolder(String folderName){
        photoFolderManagerTable.delete(Where.getInstance(false)
                .addAndWhereValue("folderName", WhereSymbolEnum.EQUAL, folderName)
                .createSQL());
    }

    public void removePhotoFile(String folderName, String resFileName){
        ResFileTable resFileTable = new ResFileTable(PHOTO_FOLDER_TABLE_NAME_SUFFIX + folderName);
        resFileTable.onCreateTableIfNotExits();
        resFileTable.delete(Where.getInstance(false)
                .addAndWhereValue("fileFolderName", WhereSymbolEnum.EQUAL, resFileName)
                .createSQL());
    }

    public boolean reNamePhotoFolder(String oldName, String newName){
        Folder newNameFolder = photoFolderManagerTable.searchByMajorKey(newName);
        if (newNameFolder != null){
            return false;
        }
        Folder oldNameFolder = photoFolderManagerTable.searchByMajorKey(oldName);
        if (oldNameFolder == null){
            return false;
        }

        oldNameFolder.setFolderName(newName);
        String whereSQL = Where.getInstance(false).addAndWhereValue("folderName", WhereSymbolEnum.EQUAL, oldName).createSQL();
        photoFolderManagerTable.update(oldNameFolder, whereSQL);

        return true;
    }

    public boolean reNameResFile(String folderName, String oldName, String newName){
        ResFileTable resFileTable = new ResFileTable(PHOTO_FOLDER_TABLE_NAME_SUFFIX + folderName);
        LogX.INSTANCE.d("xing " + resFileTable.getTableName(),true);
        ResFile newNameResFile = resFileTable.searchByMajorKey(newName);
        if (newNameResFile != null){
            return false;
        }
        ResFile oldNameResFile = resFileTable.searchByMajorKey(oldName);
        if (oldNameResFile == null){
            return false;
        }

        oldNameResFile.setFileFolderName(newName);
        String whereSQL = Where.getInstance(false).addAndWhereValue("fileFolderName", WhereSymbolEnum.EQUAL, oldName).createSQL();
        resFileTable.update(oldNameResFile, whereSQL);

        return true;
    }

    public void createNewVideoFolder(String folderName){
        Folder folder = new Folder();
        folder.setFolderName(folderName);
        folder.setFolderFatherPath(VIDEO_FOLDER_FATHER_PATH);
        folder.setSize(0);
        folder.setLength(0);
        folder.setLock(false);
        videoFolderManagerTable.saveData(folder);
    }

    public void reNameVideoFolder(String oldName, String newName){

    }

    public void addResFileToTable(String folderName, ResFile resFile){
        if (resFile.getType() == 0){

        }else if (resFile.getType() == 1){
                ResFileTable resFileTable = new ResFileTable(PHOTO_FOLDER_TABLE_NAME_SUFFIX + folderName);
                resFileTable.onCreateTableIfNotExits();
                resFileTable.saveData(resFile);
        }
    }

    public List<ResFile> getTableToResFiles(String folderName){
        ResFileTable resFileTable = new ResFileTable(PHOTO_FOLDER_TABLE_NAME_SUFFIX + folderName);
        resFileTable.onCreateTableIfNotExits();
        return resFileTable.getAllData();
    }
}
